package org.jiuling.netty.initializer;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.jiuling.basics.validation.Assertions;

/**
 * Socket连接初始化
 * @author 紫灵
 * @date 2025年07月07日 18时14分52秒
 */
public class ScoketChannelInitializer extends ChannelInitializer<NioSocketChannel> {

    private final ChannelHandler handler;
    private final ChannelHandler encoder;
    private final ChannelHandler decoder;

    public ScoketChannelInitializer(ChannelHandler handler,ChannelHandler encoder,ChannelHandler decoder) {
        Assertions.ObjectAssertions.isNotNull("处理器不能为空！",handler);
        Assertions.ObjectAssertions.isNotNull("解码器不能为空！",encoder);
        Assertions.ObjectAssertions.isNotNull("编码器不能为空！",decoder);
        this.handler = handler;
        this.encoder = encoder;
        this.decoder = decoder;
    }

    @Override
    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
        nioSocketChannel.pipeline().addLast(encoder).addLast(decoder).addLast(handler);
    }
}
